﻿using DotNetCore.CAP;
using DotNetCore.CAP.Messages;
using IMES.Common.Domain.DariusModel;
using IMES.Common.Domain.KeepModel;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using Keep.API.Appliction.Command.DepartmentFile;
using Keep.API.Appliction.Command.ProcessFile;
using Keep.API.Appliction.Command.Select;
using Keep.API.Appliction.Command.WarehouseFile;
using Keep.API.Appliction.Command.workSheetFile;
using Keep.API.Appliction.ProductionPlanFile;
using Keep.API.Common.L.Dtos;
using Keep.API.Services;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using RabbitMQ.Client;
using System.Net.Http.Json;
using System.Security.Policy;
using System.Text.Json;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace Keep.API.Controllers
{
    /// <summary>
    /// 部门控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<DepartmentController> _logger;
        private readonly ICapPublisher _capBus;

		#region 下拉框
		
		/// <summary>
		/// 下拉框产品
		/// </summary>
		/// <param name="command"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> ProductSearchSelect([FromQuery] ProductSearchSelectCommon command)
		{

			var result = await _mediator.Send(command);
			return Ok(result);
		}

		#endregion


		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="mediator"></param>
		/// <param name="logger"></param>
		public DepartmentController(IMediator mediator, ILogger<DepartmentController> logger, ICapPublisher capBus)
        {
            _mediator = mediator;
            _logger = logger;
            _capBus = capBus;
        }
        //[Route("~/checkAccount")]
        //[HttpGet]
        //public async Task<IActionResult> PublishMessage()
        //{
        //    await _capBus.PublishAsync("xxx.services.account.check", new Result { Code = 0, Messege = "Foo" });

        //    return Ok();
        //}
        /// <summary>
        /// 导入功能
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ImportFunction(ImportFunctionCommon command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }


        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DepartmentCreate(DepartmentCreateCommon command)
        {
          
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 查询部门列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns> 
        [HttpGet]
        public async Task<ResultPage<DepartmentListDto>> DepartmentList([FromQuery] DepartmentListCommon command)
        {
			//         var results = new RabbitMQService();
			//         var result = await _mediator.Send(command);
			//// 假设你想要发送的消息是基于某些业务逻辑生成的字符串
			//string messageToSend = "这里是基于业务逻辑生成的消息内容"; 
			//// 调用SendMessage方法发送消息到RabbitMQ 
			//results.SendMessage(messageToSend); 
			/*	await _capBus.PublishAsync("DepartmentCreate.services.account.check", *//*DateTime.Now);*/ 
			var result = await _mediator.Send(command);
		 
            string Department = "RabbitMQ.D";
            //向下游传送消息
            IDictionary<string,string> dicHeader = new Dictionary<string, string>();
            dicHeader.Add(key: "One", value: "What's up!ShenRui Cao!");
            dicHeader.Add(key: "Two", value: "Im Fine Thank You!");

            _capBus.Publish(name: Department, result, headers: dicHeader);


			return result;
        }
        
		[CapSubscribe(name: "RabbitMQ.D",Group ="Group.Cuijie")]
        [HttpGet]
        public async Task<IActionResult> Ceshi([FromCap] CapHeader header)
        {
            if(header !=null)
            {
                Console.WriteLine(header["One"]+"收到消息");
                Console.WriteLine(header["Two"]+ "收到消息");
            }

            return Ok(1);
        }
		/// <summary>
		/// 部门级联
		/// </summary>
		/// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DepartmentCascader(DateTime date)
        { 
			//Department entity = new Department
   //         {
   //             OrgName = "测试"
   //         };


   //         string Department = "RabbitMQ.D";
			//IDictionary<string, string> dicHeader = new Dictionary<string, string>();
			//dicHeader.Add(key: "CreateBy", value: "ShenRui_Cao");
			//dicHeader.Add(key: "Status", value: "");


			//_capBus.Publish(name: Department, entity, headers: dicHeader);
            

			//Console.Write(DateTime.Now.ToString() + "收到消息:"+   date.ToString("yyyy-MM-dd HH:mm:ss"));

			var result = await _mediator.Send(new DepartmentCascaderCommon());
            return Ok(result);
        }

        /// <summary>
        /// 仓库列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> WarehouseList([FromQuery] WarehouseListCommon comand)
        {
            var result = await _mediator.Send(comand);
            return Ok(result);
        }


        /// <summary>
        /// 仓库添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> WarehouseCreate(WarehouseCreateCommon comand)
        {
            var result = await _mediator.Send(comand);
            return Ok(result);
        }
        /// <summary>
        /// 类型下拉框数据
        /// </summary>
        /// <param name="comand"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> WTypeList()
        {
            var result = await _mediator.Send(new WTypeListCommon ());
            return Ok(result);
        }
        /// <summary>
        /// 工艺路线添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ProcessRouteCreate(ProcessRouteCreateCommon comand)
        {
            var result = await _mediator.Send(comand);
            return Ok(result);
        }
        /// <summary>
        /// 工艺路线列表
        /// </summary>
        /// <param name="comand"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> ProcessRouteList([FromQuery] ProcessRouteListCommon comand)
        {
            var result = await _mediator.Send(comand);
            return result;
        }

        /// <summary>
        /// 工序批量添加
        /// </summary>
        /// <param name="comand"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> WorkingProcedureCreate(WorkingProcedureCreateCommon comand)
        {
            var result = await _mediator.Send(comand);
            return Ok(result);
        }

        /// <summary>
        /// 工序列表
        /// </summary>
        /// <param name="comand"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Result> WorkingProcedureList([FromQuery] WorkingProcedureListCommon comand)
        {
            var result = await _mediator.Send(comand);
            return result;
        }

		/// <summary>
		/// 工单于任务批量添加
		/// </summary>
		/// <param name="comand"></param>
		/// <returns></returns> 
		[HttpPost]
		public async Task<IActionResult> workSheetCreate(workSheetCreateCommon comand)
		{
			 
			var result = await _mediator.Send(comand);
			return Ok(result);
		}
        /// <summary>
        /// 销售订单批量添加工单
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
		[CapSubscribe(name: "RabbitMQ.Order-WorkSheet", Group = "Group.Cuijie")]
		[HttpPost]
		public async Task<IActionResult> workSheetAcceptCreate([FromCap] CapHeader header)
		{
            var comand = new workSheetAcceptCreateCommon();
			if (header != null)
			{
				comand.WorkDataDtos = JsonSerializer.Deserialize<List<WorkDataDto>>(header["One"]);
			}
			Console.Write(DateTime.Now.ToString() + "收到消息:" + header["One"]);
			var result = await _mediator.Send(comand);
			return Ok(result);
		}

        /// <summary>
        /// 生产计划批量添加工单
        /// </summary>
        /// <param name="header"></param>
        /// <returns></returns>
		[CapSubscribe(name: "RabbitMQ.Production-WorkSheet", Group = "Group.Production")]
		[HttpPost]
		public async Task<IActionResult> ProductionWorkSheetHandler([FromCap] CapHeader header)
		{
			// 处理 Production-WorkSheet 消息的业务逻辑
			var productionCommand = new workSheetAcceptCreateCommon();
			if (header != null)
			{
				productionCommand.WorkDataDtos = JsonSerializer.Deserialize<List<WorkDataDto>>(header["One"]);
			}
			Console.WriteLine(DateTime.Now.ToString() + " 收到消息: " + header["One"]);
			var productionResult = await _mediator.Send(productionCommand);
			return Ok(productionResult);
		}


		/// <summary>
		/// 工单列表
		/// </summary>
		/// <param name="comand"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> workSheetList([FromQuery]workSheetListCommon comand)
		{
			var result = await _mediator.Send(comand);
			return Ok(result);
		}

		
		/// <summary>
		/// 工单于任务批量更新
		/// </summary>
		/// <param name="comand"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<IActionResult> workSheetType(workSheetTypeCommon comand)
		{
			var result = await _mediator.Send(comand);
			return Ok(result);
		}

		#region  生产计划
		
		/// <summary>
		/// 生产计划批量新增
		/// </summary>
		/// <param name="comand"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<IActionResult> ProductionPlanCreate(ProductionPlanCreateCommon comand)
		{
			var result = await _mediator.Send(comand);
			return Ok(result);
		}
         
		
		/// <summary>
		/// 生产计划列表列表
		/// </summary>
		/// <param name="comand"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> ProductionPlanList([FromQuery] ProductionPlanListCommon comand)
		{
			var result = await _mediator.Send(comand);
			return Ok(result);
		}

		/// <summary>
		/// 生产计划产品明细列表
		/// </summary>
		/// <param name="comand"></param>
		/// <returns></returns>
		[HttpGet]
		public async Task<IActionResult> PlanProductDetailsList([FromQuery] PlanProductDetailsListCommon comand)
		{
			var result = await _mediator.Send(comand);
			return Ok(result);
		}


		#endregion 
        /// <summary>
        /// 打印生产计划产品明细列表
        /// </summary>
        /// <param name="comand"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ProductPlanPrintList([FromQuery] ProductPlanPrintCommon comand)
        {
            var result = await _mediator.Send(comand);
            return Ok(result);
        }
    }
}
